// 移动动画
let animation = wx.createAnimation({});
// 提示音
let innerAudioContext = wx.createInnerAudioContext()
// innerAudioContext.src = '/image/5383.mp3'

Page({
  data: {
    scaned: 0,
    type: null,
    id: null,
    process: null,
    way: null,
    from: null,
    showMpDialog: false,
    currentScanDetails: "",
    buttons: [{
      text: '上班'
    }, {
      text: '下班'
    }]
  },
  onLoad: function (options) {
    console.log("options", options);
    this.setData({
      type: options.type,
      process: options.process,
      from: options.from
    })

    if (this.data.type == 1) {
      this.setData({
        id: options.id,
        way: options.id == 'null' ? options.way : null
      })
    }
    this.donghua()
  },
  onShow() {
    this.setData({
      scaned: 0
    })
  },
  tapDialogButton(e) {
    console.log("考勤进入");
    console.log("考勤进入", e.detail.index);
    if (e.detail.index == 0) {
      global.$api.post({
        url: '/api/userInfo/scanQrCode',
        data: {
          str: this.data.currentScanDetails,
          type: 1
        }
      }).then(res => {
        if (res.code == 200) {
          this.setData({
            showMpDialog: false
          })
          wx.showToast({
            title: '上班打卡成功',
            icon: 'success',
            success: () => {
              setTimeout(() => {
                wx.navigateBack()
              }, 1000);
            }
          })
        } else {
          setTimeout(() => {
            wx.navigateBack()
          }, 1000);
        }
      })
    } else {
      global.$api.post({
        url: '/api/userInfo/scanQrCode',
        data: {
          str: this.data.currentScanDetails,
          type: 2
        }
      }).then(res => {
        console.log(res);
        if (res.code == 200) {
          this.setData({
            showMpDialog: false
          })
          wx.showToast({
            title: '下班打卡成功',
            icon: 'success',
            success: () => {
              setTimeout(() => {
                wx.navigateBack()
              }, 1000);
            }
          })
        } else {
          setTimeout(() => {
            wx.navigateBack()
          }, 1000);
        }
      })
    }
    // wx.showModal({
    //   title: '提示',
    //   content: '考勤打卡',
    //   cancelColor: '#576B95',
    //   cancelText: "上班",
    //   confirmColor: "#d81e06",
    //   confirmText: "下班",
    //   success(res) {
    //     if (res.confirm) { //下班
    //       console.log('用户点击确定');

    //     } else if (res.cancel) { //上班
    //       console.log('用户点击取消')

    //     }
    //   }
    // })
  },
  //点击蒙层关闭
  bindclose(e) {
    wx.navigateBack()
  },
  donghua() {
    var that = this;
    // 控制向上还是向下移动
    let m = true

    setInterval(function () {
      if (m) {
        animation.translateY(250).step({
          duration: 3000
        })
        m = !m;
      } else {
        animation.translateY(0).step({
          duration: 3000
        })
        m = !m;
      }

      that.setData({
        animation: animation.export()
      })
    }.bind(this), 3000)
  },
  cameraError() {
    wx.navigateBack({
      delta: 1,
    })
  },
  scancode(e) {
    // 提示音
    console.log(e)
    let that = this;
    // 校验扫描结果，并处理
    let scaned = this.data.scaned;
    console.log(scaned)
    // wx.showToast({
    //   title: "scaned" + scaned,
    //   icon: 'none'
    // })       
    if (!scaned) {
      console.log(111111111111111111)
      // innerAudioContext.play()
      this.setData({
          scaned: !0
        },
        function () {
          let detail = e.detail.result;
          console.log("111111111111---------", that.data.type);
          console.log("222" + detail);
          if (that.data.type == 10) { //扫码成功
            global.$api.post({
              url: '/api/skirt/judgeSkirtOrderProcess',
              data: {
                handOrderNum: detail
              }
            }).then(res => {
              if (res.code == 200) {
                if (res.data == true) {
                  wx.navigateTo({
                    url: '../checkname/checkname?handOrderNum=' + detail
                  })
                } else {
                  wx.navigateTo({
                    url: '../process/process'
                  })
                }
              }else{
                setTimeout(() => {
                  wx.navigateBack();
                }, 1500);
              }
            })
          } else if (this.data.type == 99) { //考勤进入
            this.setData({
              showMpDialog: true,
              currentScanDetails: detail
            })
          } else if (this.data.type == 1) {
            if (this.data.process == 1) {
              wx.redirectTo({
                url: `../searchresult/searchresult?orderNum=${detail}&&processId=${this.data.id}&from=${this.data.from}`,
              })
            } else {
              this.data.way == null ? this.startProduction(detail) : this
                .ChangingorRefunding(
                  detail)
            }
          } else if(this.data.type == 11){
              // 质量检验
              wx.navigateTo({
                url: `../qualityCheck/qualityCheck?handOrderNum=${detail}`,
              })
          } else {
            wx.navigateTo({
              url: `../workorderDetal/workorderDetal?odd=${detail}`,
            })
          }
        })
    }
  },
  async startProduction(workNumber) {
    var data = await global.$api.post({
      url: 'api/startProduction/isLastProcess',
      data: {
        orderNum: workNumber,
        processId: this.data.id
      }
    })
    if (data.code == 200) {
      console.log(data.data)
      if (data.data == 1) {
        innerAudioContext.src = '/image/2.mp3'
        innerAudioContext.play()
        wx.showToast({
          title: '要发货',
          icon: 'none'
        })
        console.log('data为1')
        setTimeout(() => {
          this.setData({
            scaned: 0
          })
        }, 2000);
      } else if (data.data == 2) {
        innerAudioContext.src = '/image/1.mp3'
        innerAudioContext.play()
        // wx.showToast({
        //   title: '完成此订单',
        //   icon:'none'
        // })
        console.log('data为2')
        setTimeout(() => {
          this.setData({
            scaned: 0
          })
        }, 2000);
      } else {
        innerAudioContext.src = '/image/3.mp3'
        innerAudioContext.play()
        wx.showToast({
          title: '该订单已扫码完成,请勿重新扫描',
          icon: 'none'
        })
        console.log('data为3')
        setTimeout(() => {
          this.setData({
            scaned: 0
          })
        }, 2000);
      }
      // setTimeout(() => {
      //   wx.showToast({
      //     title: '完成此订单',
      //     icon:'none'
      //   })
      //   this.setData({
      //     scaned:0
      //   })
      // }, 500);
      // wx.navigateBack({
      //   delta: 1,
      // })
    } else {
      setTimeout(() => {
        wx.showToast({
          title: data.message,
          icon: 'none'
        })
        this.setData({
          scaned: 0
        })
      }, 500);
      // wx.navigateBack({
      //   delta: 1,
      // })
    }
  },
  async ChangingorRefunding(num) {
    if (this.data.way == 4) {
      var data = await global.$api.post({
        url: 'api/userInfo/lookCashSale',
        data: {
          orderNum: num,
          type: this.data.way
        }
      })
      if (data.code == 200) {
        console.log(data)
        if (data.data == 1) {
          innerAudioContext.src = '/image/2.mp3'
          innerAudioContext.play()
          wx.showToast({
            title: '要发货',
            icon: 'none'
          })
        } else {
          innerAudioContext.src = '/image/1.mp3'
          innerAudioContext.play()
          // wx.showToast({
          //   title: '有现货',
          //   icon:'none'
          // })
        }
        setTimeout(() => {
          wx.navigateBack({
            delta: 1,
          })
        }, 1000)
      } else {
        wx.showToast({
          title: data.message,
          icon: 'none'
        })
      }
    } else {
      var data = await global.$api.post({
        url: 'api/userInfo/changingOrRefunding',
        data: {
          orderNum: num,
          type: this.data.way
        }
      })
      if (data.code == 200) {
        if (this.data.way == 2) {
          wx.showToast({
            title: '换货成功',
            icon: 'none'
          })
          setTimeout(() => {
            wx.navigateBack({
              delta: 1,
            })
          }, 300)
        } else {
          console.log(data)
          if (data.data == 1) {
            innerAudioContext.src = '/image/2.mp3'
            innerAudioContext.play()
            wx.showToast({
              title: '要发货',
              icon: 'none'
            })
          } else {
            innerAudioContext.src = '/image/1.mp3'
            innerAudioContext.play()
            // wx.showToast({
            //   title: '有现货',
            //   icon:'none'
            // })
          }
          setTimeout(() => {
            wx.navigateBack({
              delta: 1,
            })
          }, 1000)
        }
        // setTimeout(()=>{
        //   this.setData({
        //     scaned:0
        //   })

        // },500)
      } else {
        this.showToast({
          title: data.message,
          icon: 'none'
        })
        setTimeout(() => {

          this.setData({
            scaned: 0
          })
        }, 500)
      }
    }
  },
})